package com.htt.app.doctruyendemkhuya_joyfm.sqlite;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.htt.app.doctruyendemkhuya_joyfm.object.Show;
import com.htt.app.doctruyendemkhuya_joyfm.utils.Log;

public class DAOShow {

	
	public final String TAG = "DAOShow";
	
	private SQLiteDatabase db;
	private MySQLiteHelper dbHelper;
	
	private String[] allColumns = { MySQLiteHelper.showID,
		      MySQLiteHelper.showName, MySQLiteHelper.showDes,
		      MySQLiteHelper.showIcon, MySQLiteHelper.showSource,
		      MySQLiteHelper.showOrder, MySQLiteHelper.showStatusFavourite };
	
	public DAOShow(Context context) {
		if(MySQLiteHelper.myDBHelper != null)
			dbHelper = MySQLiteHelper.myDBHelper;
		else {
			dbHelper = new MySQLiteHelper(context);
			MySQLiteHelper.myDBHelper = dbHelper;
		}
	}
	
	public void open() throws SQLException{
		synchronized (MySQLiteHelper.Lock) {
			Log.w(TAG, "open " + TAG);
			if(MySQLiteHelper.myDB != null && MySQLiteHelper.myDB.isOpen())
				db = MySQLiteHelper.myDB;
			else {
				db = dbHelper.getWritableDatabase();
				MySQLiteHelper.myDB = db;
			}
		}

	}
	
	public void close() {
		
		synchronized (MySQLiteHelper.Lock) {
			Log.w(TAG, "close " + TAG);
			
			if(dbHelper != null) {
				Log.w(TAG, "close success " + TAG);
				if(db != null)
					db.close();
				if(MySQLiteHelper.myDB != null && MySQLiteHelper.myDB.isOpen())
					MySQLiteHelper.myDB.close();
				
				dbHelper.close();
				MySQLiteHelper.myDB = null;
				MySQLiteHelper.myDBHelper = null;
			}
		}
		
	}
	
	// Adding new Show
    public void insertShow(Show show) {
 
        ContentValues values = new ContentValues();
        values.put(MySQLiteHelper.showID, show.getId()); 
        values.put(MySQLiteHelper.showName, show.getName());
        values.put(MySQLiteHelper.showDes, show.getDesc());
        values.put(MySQLiteHelper.showIcon, show.getIcon());
        values.put(MySQLiteHelper.showSource, show.getSource());
        values.put(MySQLiteHelper.showOrder, show.getOrder());
        values.put(MySQLiteHelper.showStatusFavourite, show.getStatusFavourite());
        
        // Inserting Row
        if(db.insert(MySQLiteHelper.showTable, null, values)!=-1)
        	Log.w(TAG, "insert sucess");
    }
    
    // delete show
    public void deleteShow(Show show) {
    	db.delete(MySQLiteHelper.showTable, MySQLiteHelper.showID + " = " + show.getId(), null);
    }
    
    public void deleteAllShow() {
    	int doneDelete = 0;
        doneDelete = db.delete(MySQLiteHelper.showTable, null, null);
        Log.w(TAG, Integer.toString(doneDelete));
    }
    
    
 // check show
    public boolean checkShow(Show show) {
    	Cursor cursor = db.query(MySQLiteHelper.showTable, allColumns, MySQLiteHelper.showID + " = " + show.getId(), 
    			null, null, null, null);
    	if(cursor != null && cursor.getCount() > 0) {
	    	return true; 
    	} else
    		return false;
    }

  	
    // getting all
    public List<Show> getAllShow() {
    	List<Show> list = new ArrayList<Show>();
    	
    	String selectQuery = "SELECT  * FROM " + MySQLiteHelper.showTable;
    	Cursor cursor = db.rawQuery(selectQuery, null);
    	if(cursor != null && cursor.getCount() > 0) {
	    	if(cursor.moveToFirst()) {
	    		do{
	    			Show show = cursorToShow(cursor);
	    			list.add(show);
	    		} while(cursor.moveToNext());
	    	}
	    	
	    	if(cursor != null && !cursor.isClosed())
	    		cursor.close();
	    	return list;
    	} else {
    		return null;
    	}
    }
    
    
 // Getting  Count
    public int getShowsCount() {	
        String countQuery = "SELECT  * FROM " + MySQLiteHelper.showTable;	
        Cursor cursor = db.rawQuery(countQuery, null);	
        cursor.close();	
        
        // return count
        return cursor.getCount();
    }
    
    // Show from cursor
    private Show cursorToShow(Cursor cursor) {
    	Show show = new Show();
    	show.setId(cursor.getInt(0));
    	show.setName(cursor.getString(1));
    	show.setDesc(cursor.getString(2));
    	show.setIcon(cursor.getString(3));
    	show.setSource(cursor.getString(4));
    	show.setOrder(cursor.getInt(5));
    	show.setStatusFavourite(cursor.getInt(6));
    	
        return show;
      }

}
